package com.xianniu.ltc.service.impl;

import com.rebuild.core.UserContextHolder;
import com.rebuild.core.metadata.EntityHelper;
import com.xianniu.ltc.mapper.TRbshangji4Mapper;
import com.xianniu.ltc.model.po.report.ChartCommonPermissionsPO;
import com.xianniu.ltc.model.po.report.ReportLeadPO;
import com.xianniu.ltc.model.vo.CashTransferSchemesVO;
import com.xianniu.ltc.model.vo.StatisticsXYVO;
import com.xianniu.ltc.model.vo.statistics.BenNianQianYueEVO;
import com.xianniu.ltc.model.vo.statistics.ShangJiStatisticsVo;
import com.xianniu.ltc.model.vo.statistics.StatisticsYVO;
import com.xianniu.ltc.model.vo.statistics.WebStatisticsXYVO;
import com.xianniu.ltc.service.NicheCodeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.SimpleDateFormat;
import java.util.*;
@Slf4j
@Service
public class NicheCodeImpl implements NicheCodeService {
    @Resource
    TRbshangji4Mapper tRbshangji4Mapper;

    @Override
    public WebStatisticsXYVO queryNowYearShangJi( ReportLeadPO po ){
        // 依据 周 , 月 , 本年& 上一年 , 自定义 分组统计
        Integer timeGroup = po.getTimeGroup();
        switch(timeGroup){
            case 1:
                return queryNowYearShangJiByWeek( po );
            case 2:
                return queryNowYearShangJiByMonth( po );
            case 3:
                return queryNowYearShangJiByYear( po );
            case 4:
                return queryNowYearShangJiByStartAndEndTime( po );
            default:
                WebStatisticsXYVO webVo4 = new WebStatisticsXYVO();
                return webVo4;
        }
    }

    /**
     * 新增商机-数量统计-按周
     * @param po
     * @return
     */
    @Override
    public WebStatisticsXYVO queryNowYearShangJiByWeek( ReportLeadPO po ){

        // 查询今天属于本年第几周
        Calendar c = Calendar.getInstance();
        // 设置每周的开始日期
        c.setFirstDayOfWeek(Calendar.MONDAY);
        c.setTime(new Date());
        c.setMinimalDaysInFirstWeek(7);
        // 当前周
        int week = c.get(Calendar.WEEK_OF_YEAR);
        Map<String, String> weekNameMap = new HashMap<String, String>();
        int[] weekList = new int[5];
        for ( int i=0; i<=4; i++ ){
            Integer tWeek = week-i;
            weekList[i] = tWeek;
            if( i==0 ){
                weekNameMap.put(tWeek.toString(), "本周");
            }else if(i>0){
                weekNameMap.put(tWeek.toString(), "前"+i+"周");
            }
        }
        // 依据条件查询数据列表
        List<ShangJiStatisticsVo> weekData = tRbshangji4Mapper.queryNowYearShangJiByWeek( po, weekList );

        // 地域名称作为Y轴data集合
        List<ShangJiStatisticsVo> yData = new ArrayList<ShangJiStatisticsVo>();
        List<String> xData = new ArrayList<String>();
        // 计算总数
        BigDecimal total = BigDecimal.ZERO;
        for( int i=0; i<weekList.length; i++ ){
            BigDecimal weekValue = BigDecimal.ZERO;
            Integer weekNum = weekList[i];
            Integer weekTotal = 0;
            for (ShangJiStatisticsVo item:weekData) {
                String label = item.getLabel();
                if( weekNum.toString().equals(label) ){
                    weekValue = item.getJinE();
                    weekTotal = item.getZongShu();
                }
            }
            // 保留两位
            //weekValue = weekValue.setScale(2, RoundingMode.HALF_UP);
            total = total.add( weekValue );
            xData.add(weekNameMap.get(weekNum.toString()));

            ShangJiStatisticsVo sjsVo = new ShangJiStatisticsVo();
            sjsVo.setLabel(weekNameMap.get(weekNum.toString()));
            sjsVo.setJinE(weekValue);
            sjsVo.setZongShu(weekTotal);
            yData.add(sjsVo);
        }

        // 设置Y轴数据
        StatisticsYVO trsyVo = new StatisticsYVO();
        trsyVo.setData( yData );
        trsyVo.setName( "商机新增数量" );

        // 设置整个图标XY轴数据
        WebStatisticsXYVO webVo = new WebStatisticsXYVO();
        webVo.setXAxis(xData);
        webVo.setYyyAxis(trsyVo);
        // 保留两位
        //total = total.setScale(2, RoundingMode.HALF_UP );
        webVo.setTotal( total.toString() );
        return webVo;
    }

    /**
     * 新增商机-数量统计-按月
     * @param po
     * @return
     */
    @Override
    public WebStatisticsXYVO queryNowYearShangJiByMonth( ReportLeadPO po ){

        // 依据条件查询数据列表
        List<ShangJiStatisticsVo> monthData = tRbshangji4Mapper.queryNowYearShangJiByMonth( po );

        // 地域名称作为Y轴data集合
        //List<String> yData = new ArrayList<String>();
        List<String> xData = new ArrayList<String>();
        // 计算总数
        BigDecimal total = BigDecimal.ZERO;
        for (ShangJiStatisticsVo item:monthData) {
            String yearAndMonth = item.getLabel();
            // 年
            String year = yearAndMonth.substring(0,4);
            // 月
            String month = yearAndMonth.substring(4,6);
            total = total.add( item.getJinE() );

            // 金额
            //BigDecimal jine = item.getJine();
            // 保留两位
            //jine = jine.setScale(2, RoundingMode.HALF_UP);
            xData.add(year+"年"+month+"月");
            //yData.add(jine.toString());
        }

        // 设置Y轴数据
        StatisticsYVO trsyVo = new StatisticsYVO();
        trsyVo.setData( monthData );
        trsyVo.setName( "商机新增数量" );

        // 设置整个图标XY轴数据
        WebStatisticsXYVO webVo = new WebStatisticsXYVO();
        webVo.setXAxis(xData);
        webVo.setYyyAxis(trsyVo);

        // 保留两位
        //total = total.setScale(2, RoundingMode.HALF_UP);
        webVo.setTotal( total.toString() );
        return webVo;
    }

    /**
     * 新增商机-数量统计-按年
     * @param po
     * @return
     */
    @Override
    public WebStatisticsXYVO queryNowYearShangJiByYear( ReportLeadPO po ){

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        // 上一年开始时间
        Calendar c = Calendar.getInstance();
        c.add(Calendar.YEAR, -1);
        c.set(c.get(c.YEAR), 0,1);
        String dateStart = simpleDateFormat.format(c.getTime())+" 00:00:00";
        log.info("商机新增-按年-上一年开始时间 == {}",dateStart);
        po.setCreateDateS( dateStart );
        log.info("商机新增-按年-po == {}",po);
        // 依据条件查询数据列表
        List<ShangJiStatisticsVo> yearData = tRbshangji4Mapper.queryNowYearShangJiByYear( po );

        // 地域名称作为Y轴data集合

        List<String> xData = new ArrayList<String>();
        // 计算总数
        BigDecimal total = BigDecimal.ZERO;

        for (ShangJiStatisticsVo item:yearData) {
            String label = item.getLabel();
            BigDecimal jine = item.getJinE();
            total = total.add( jine );
            xData.add(label+"年");
        }

        // 设置Y轴数据
        StatisticsYVO trsyVo = new StatisticsYVO();
        trsyVo.setData( yearData );
        trsyVo.setName( "商机新增数量" );

        // 设置整个图标XY轴数据
        WebStatisticsXYVO webVo = new WebStatisticsXYVO();
        webVo.setXAxis(xData);
        webVo.setYyyAxis(trsyVo);
        // 保留两位
        //total = total.setScale(2, RoundingMode.HALF_UP );
        webVo.setTotal( total.toString() );
        return webVo;
    }


    /**
     * 新增商机-数量统计-按起止时间
     * @param po
     * @return
     */
    @Override
    public WebStatisticsXYVO queryNowYearShangJiByStartAndEndTime( ReportLeadPO po ){
        String startTime = po.getCreateDateS();
        po.setCreateDateS( startTime+" 00:00:00" );
        String endTime = po.getCreateDateE();
        po.setCreateDateE( endTime+" 23:59:59" );
        // 依据条件查询数据列表
        List<ShangJiStatisticsVo> startAndEndTimeData = tRbshangji4Mapper.queryNowYearShangJiByStartAndEndTime( po );

        // 地域名称作为Y轴data集合

        List<String> xData = new ArrayList<String>();
        // 计算总数
        BigDecimal total = BigDecimal.ZERO;

        for (ShangJiStatisticsVo item:startAndEndTimeData) {
            String  label = item.getLabel();
            BigDecimal jine = item.getJinE();
            total = total.add( jine );
            xData.add(label);
        }

        // 设置Y轴数据
        StatisticsYVO trsyVo = new StatisticsYVO();
        trsyVo.setData( startAndEndTimeData );
        trsyVo.setName( "商机新增数量" );

        // 设置整个图标XY轴数据
        WebStatisticsXYVO webVo = new WebStatisticsXYVO();
        webVo.setXAxis(xData);
        webVo.setYyyAxis(trsyVo);
        // 保留两位
        //total = total.setScale(2, RoundingMode.HALF_UP );
        webVo.setTotal( total.toString() );
        return webVo;
    }

    /**
     * 那行业分布
     * @param po
     * @return
     */
    @Override
    public WebStatisticsXYVO queryNowYearShangJiByIndustry( ReportLeadPO po ){
        // 依据条件查询数据列表
        List<ShangJiStatisticsVo> startAndEndTimeData = tRbshangji4Mapper.queryNowYearShangJiByIndustry( po );

        // 地域名称作为Y轴data集合

        List<String> xData = new ArrayList<String>();
        // 计算总数
        BigDecimal total = BigDecimal.ZERO;

        for (ShangJiStatisticsVo item:startAndEndTimeData) {
            String  label = item.getLabel();
            BigDecimal jine = item.getJinE();
            total = total.add( jine );
            xData.add(label);
        }

        // 设置Y轴数据
        StatisticsYVO trsyVo = new StatisticsYVO();
        trsyVo.setData( startAndEndTimeData );
        trsyVo.setName( "商机新增数量" );

        // 设置整个图标XY轴数据
        WebStatisticsXYVO webVo = new WebStatisticsXYVO();
        webVo.setXAxis(xData);
        webVo.setYyyAxis(trsyVo);
        // 保留两位
        //total = total.setScale(2, RoundingMode.HALF_UP );
        webVo.setTotal( total.toString() );
        return webVo;
    }

}
